#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1100;
int c[maxn];
struct edge
{
	int x,y;
}e[maxn*maxn+1];
int n,m,k;
int low(int x)
{
	return x&(x^(x-1));
}
int sum(int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=c[x];
		x-=low(x);
	}
	return ans;
}
void add(int i,int x)
{
	while(i<=m)
	{
		c[i]+=x;
		i+=low(i);
	}
}
bool cmp(edge a,edge b)
{
	if(a.x==b.x)return a.y<b.y;
	return a.x<b.x;
}
int main()
{
	freopen("test.in","r",stdin);
	//freopen("test.out","w",stdout);
	int t;
	scanf("%d",&t);
	for(int cas=1;cas<=t;cas++)
	{
		scanf("%d%d%d",&n,&m,&k);
		memset(c,0,sizeof(c));
		for(int i=1;i<=k;i++)
			scanf("%d%d",&e[i].x,&e[i].y);
		sort(e+1,e+k+1,cmp);
		long long ans=0;
		add(e[k].y,1);
		for(int i=k-1;i>=1;i--)
		{
			ans+=sum(e[i].y-1);
			add(e[i].y,1);
		}
		printf("Test case %d: %lld\n",cas,ans);
	}
	return 0;
}

